package com.cool.reggie.modules.addressBook.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cool.reggie.modules.addressBook.entity.AddressBookEntity;
import com.cool.reggie.modules.addressBook.service.AddressBookService;
import com.cool.reggie.modules.user.service.UserService;
import com.cool.reggie.utils.BaseContext;
import com.cool.reggie.utils.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author 1950735817@qq.com
 * @since 2022-07-18
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public ResultData list() {

        LambdaQueryWrapper<AddressBookEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBookEntity::getUserId, BaseContext.getCurrentId());
        List<AddressBookEntity> list = addressBookService.list(queryWrapper);
        return ResultData.ok(list);
    }


    @PostMapping
    public ResultData addAddress(@RequestBody AddressBookEntity addressBook) {

        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return ResultData.ok();
    }

    @PutMapping("/default")
    public ResultData defaultAddress(@RequestBody AddressBookEntity addressBook) {

        // 所有地址置0
        LambdaQueryWrapper<AddressBookEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBookEntity::getUserId, BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookService.update(addressBook, queryWrapper);

        // 设置地址为1
        addressBook.setIsDefault(1);
        addressBook.setUpdateUser(BaseContext.getCurrentId());
        addressBook.setUpdateTime(new Date());
        // 使用手写语句
        addressBookService.updateDefaultAddress(addressBook);

        return ResultData.ok(addressBook);
    }
    @GetMapping("/default")
    public ResultData getDefaultAddress() {
        LambdaQueryWrapper<AddressBookEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBookEntity::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBookEntity::getIsDefault, 1);

        AddressBookEntity defaultAddress = addressBookService.getOne(queryWrapper);
        return ResultData.ok(defaultAddress);
    }

    @GetMapping("/{id}")
    public ResultData defaultAddress(@PathVariable("id") Long id) {
        LambdaQueryWrapper<AddressBookEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(id.toString()), AddressBookEntity::getId, id);
        AddressBookEntity addressBook = addressBookService.getOne(queryWrapper);

        return ResultData.ok(addressBook);
    }

    @PutMapping
    public ResultData updateAddress(@RequestBody AddressBookEntity addressBookEntity) {
        addressBookService.updateById(addressBookEntity);
        return ResultData.ok();
    }

}
